package me.yxy.mobanddd.finder;

import me.yxy.mobanddd.components.MPage;
import me.yxy.mobanddd.components.PageRepository;
import me.yxy.mobanddd.domain.warehouse.Warehouse;
import me.yxy.mobanddd.domain.warehouse.WarehouseRepository;
import me.yxy.mobanddd.finder.view.warehouse.WarehouseConsoleGroupView;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 仓库查询处理类
 */
@Service
@Transactional(readOnly = true)
public class WarehouseFinder {

    @Autowired
    WarehouseRepository warehouseRepository;
    @Autowired
    private PageRepository pageRepository;

    /**
     * 分组获取每日仓库订单统计，同样支持分页
     */
    public MPage<WarehouseConsoleGroupView> countOrdersByWarehouse(String date, Pageable pageable){

        //采用原生sql，只有2种方案：
        //2.采用EntityManager动态设置nativeSql，和上面正好相反，sql可以动态，但没办法返回Page对象

        //1.采用repository的@query，这样可以很哈的结合Spring的pageable，弊端是没办法动态设置nativeSql
        Page p =  warehouseRepository.groupTransportCount(DateTime.now().toDate(),pageable);
//        Page p2 =  warehouseRepository.groupTransportCountToMap(DateTime.now().toDate(),pageable);


        //TODO 最好挖掘一下sessionFactory，来支持最原始的sql方式，然后反射生成vo list
        return MPage.of(p);
    }


}
